const state = {
  //  登录状态
  loginStatus: false,
  // token
  token: null,
  // 用户的信息
  userInfo: {},
  // 人脸token
  access_token: "",
}

const getters = {
  count: (state) => {
    return state.count
  },
}

const mutations = {
  // 用户登录时
  userLogin(state, user) {
    console.log(state, user)
    state.loginStatus = true
    state.token = user.token
    state.userInfo = user.data[0]
    console.log(state)
    // 持久存储
    localStorage.setItem("UserShopInfo", JSON.stringify(user))
  },
  // 读取时
  userInit(state) {
    let userInfo = JSON.parse(localStorage.getItem("UserShopInfo"))
    if (userInfo) {
      state.loginStatus = true
      state.token = userInfo.token
      state.userInfo = userInfo
    }
    return userInfo
  },

  // 退出登录
  loginOut(state) {
    console.log("退出方法执行了")
    state.loginStatus = false
    state.token = null
    state.userInfo = {}
    localStorage.removeItem("UserShopInfo")
  },
  decrementCount(state, payload) {
    state.count -= payload.amout
  },
}

const actions = {
  incrementCountAsync({ commit }) {
    setTimeout(() => {
      commit("incrementCount")
    }, 2000)
  },
  decrementCountAsync({ commit }, payload) {
    setTimeout(() => {
      commit("decrementCount", payload)
    }, 1000)
  },
}

export default {
  state,
  getters,
  mutations,
  actions,
}
